<?php
class OrdersAction extends BaseAction
{
    public function __construct()
    {
		parent::__construct();
		$this->menuid = 10;
	}
	
    public function Index()
    {
        $orders_list = D("order")->order("id desc")->findAll();
		$this->assign("orders_list", $orders_list);
        $this->assign('sub_menu', $this->admin_menu($this->menuid));
        $this->display();
    }
    
    public function sync_taobao_trades()
    {
        //获取上次同步的时间
        $config_mod = D('config');
        $now = date('Y-m-d H:i:s', time());
        $start_time = '';
        $last_sync_time_result = $config_mod->field('data')->where("name='taobao_sync_time'")->find();
        $last_sync_time = $last_sync_time_result['data'];
        if ($last_sync_time) {
            $start_time = $last_sync_time;
        }
        $total_results = $this->sync_taobao_trades_page(1, $start_time);
        $page_count = ceil($total_results/100);
        for ($page=2; $page<=$page_count; $page++) {
            $this->sync_taobao_trades_page($page, $start_time);
        }
        $config_mod->where("name='taobao_sync_time'")->save(array('data'=>$now));
        echo '<div style="text-align:center;height:50px; line-height:50px;">同步完成</div>';
        exit;
    }
    
    public function sync_taobao_trades_page($page=1, $start_time)
    {
        $trades_mod = D('trades');
        $order_mod = D('order');
        //淘宝订单
        $req = $this->taobao->load_api('TradesSoldGetRequest');
        $req->setFields("seller_nick, buyer_nick, title, type, created, tid, seller_rate, buyer_rate, status, payment, discount_fee, adjust_fee, post_fee, total_fee, pay_time, end_time, modified, consign_time, buyer_obtain_point_fee, point_fee, real_point_fee, received_payment, commission_fee, pic_path, num_iid, num, price, cod_fee, cod_status, shipping_type, receiver_name, receiver_state, receiver_city, receiver_district, receiver_address, receiver_zip, receiver_mobile, receiver_phone, orders");
        if ($start_time) {
            $req->setStartCreated($start_time);
        }
        //$req->setStartCreated("2000-01-01 00:00:00");
        //$req->setEndCreated("2000-01-01 23:59:59");
        //$req->setStatus("ALL_WAIT_PAY");
        //$req->setBuyerNick("zhangsan");
        //$req->setType("game_equipment");
        //$req->setRateStatus("RATE_UNBUYER");
        //$req->setTag("time_card");
        $req->setPageNo(1);
        $req->setPageSize(100);
        $resp = $this->taobao->execute($req);
        //写入到数据库
        $resp_arr = $this->simplexml_obj2array($resp);
        
        $total_results = $resp_arr['resp_arr']; //获得总数
        $total_page = $resp_arr['resp_arr']/100; //计算页数
        
        $trades = array();
        if (isset($resp_arr['trades']['trade'])) {
            if (isset($resp_arr['trades']['trade'][0]) && is_array($resp_arr['trades']['trade'][0])) {
                $trades = $resp_arr['trades']['trade'];
            } else {
                $trades[] = $resp_arr['trades']['trade'];
            }
        }
        $trades = $this->saddslashes($trades);
        foreach ($trades as $val) {
            //插入交易表
            $data['tid'] = isset($val['tid']) ? $val['tid'] : '';
            $data['seller_nick'] = isset($val['seller_nick']) ? $val['seller_nick'] : '';
            $data['buyer_nick'] = isset($val['buyer_nick']) ? $val['buyer_nick'] : '';
            $data['title'] = isset($val['title']) ? $val['title'] : '';
            $data['payment'] = isset($val['payment']) ? $val['payment'] : '';
            $data['status'] = isset($val['status']) ? $val['status'] : '';
            $data['created'] = isset($val['created']) ? $val['created'] : '';
            $data['receiver_name'] = isset($val['receiver_name']) ? $val['receiver_name'] : '';
            $data['receiver_state'] = isset($val['receiver_state']) ? $val['receiver_state'] : '';
            $data['receiver_city'] = isset($val['receiver_city']) ? $val['receiver_city'] : '';
            $data['receiver_district'] = isset($val['receiver_district']) ? $val['receiver_district'] : '';
            $data['receiver_address'] = isset($val['receiver_address']) ? $val['receiver_address'] : '';
            $data['receiver_zip'] = isset($val['receiver_zip']) ? $val['receiver_zip'] : '';
            $data['receiver_mobile'] = isset($val['receiver_mobile']) ? $val['receiver_mobile'] : '';
            $data['end_time'] = isset($val['end_time']) ? $val['end_time'] : '';
            $trades_mod->add($data);
            
            //插入订单表
            $orders = array();
            if (isset($val['orders']['order'])) {
                if (isset($val['orders']['order'][0]) && is_array($val['orders']['order'][0])) {
                    $orders = $val['orders']['order'];
                } else {
                    $orders[] = $val['orders']['order'];
                }
                
                foreach ($orders as $ov) {
                    $s = $order_mod->add(array(
                        'tid' => $val['tid'],
                        'oid' => $ov['oid'],
                        'item_id' => $ov['num_iid'],
                        'title' => $ov['title'],
                        'price' => $ov['price'],
                        'num' => $ov['num'],
                        'total_fee' => $ov['total_fee'],
                    ));
                }
            }
        }
        return $resp_arr['total_results'];
    }
}
?>